package com.teamb6.shop.controller.front;

import com.teamb6.shop.domain.*;
import com.teamb6.shop.service.GoodsService;
import com.teamb6.shop.service.ShopCartService;
import com.teamb6.shop.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class ShopCartController {
    @Autowired
    private ShopCartService shopCartService;

    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/showcart")
    public String showCart(HttpSession session) {
        User user = (User) session.getAttribute("user");
//        User user = new User();
//        user.setUserid(1);
        if(user == null) {
            return "redirect:/users/login";
        }
        return "cart";
    }


    @RequestMapping("/cartjson")
    @ResponseBody
    public Msg getCart(HttpSession session) {
        User user = (User) session.getAttribute("user");
        List<Goods> goods = new ArrayList<>();
        int goodsSum = 0;

//        User user = new User();
//        user.setUserid(1);
        if(user == null) {
            return Msg.fail("请先登录");
        }

        //获取当前用户的购物车信息
        ShopCartExample shopCartExample = new ShopCartExample();
        shopCartExample.or().andUseridEqualTo(user.getUserid());
        List<ShopCart> shopCart = shopCartService.selectByExample(shopCartExample);

        //获取购物车中的商品信息
        List<Goods> goodsAndImage = new ArrayList<>();
        for (ShopCart cart:shopCart) {
            Goods good = goodsService.selectById(cart.getGoodsid());
            List<ImagePath> imagePathList = goodsService.findImagePath(good.getGoodsid());
            good.setImagePaths(imagePathList);
            good.setNum(cart.getGoodsnum());
            goodsAndImage.add(good);

            goodsSum++;
        }
        session.setAttribute("goodsSum",goodsSum);

        return Msg.success("查询成功").add("shopcart",goodsAndImage);
    }

    @RequestMapping(value = "/deleteCart/{goodsid}", method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteCart(@PathVariable("goodsid")Integer goodsid, HttpSession session) {
        User user = (User) session.getAttribute("user");
        int goodsSum = (int)session.getAttribute("goodsSum");

//        User user = new User();
//        user.setUserid(1);
        if(user == null) {
            return Msg.fail("请先登录");
        }
        goodsSum--;
        session.setAttribute("goodsSum",goodsSum);
        ShopCart shopCart = shopCartService.selectCartByKey(new ShopCartKey(user.getUserid(),goodsid));
        Goods goods = goodsService.selectById(goodsid);

        shopCartService.deleteByKey(new ShopCartKey(user.getUserid(), goodsid));
        return Msg.success("查询成功").add("good_price",shopCart.getGoodsnum()*goods.getPrice());
    }

    @RequestMapping("/cart/update")
    @ResponseBody
    public Msg updateCart(Integer goodsid,Integer num,HttpSession session) {
        User user = (User) session.getAttribute("user");
//        User user = new User();
//        user.setUserid(1);
        if(user == null) {
            return Msg.fail("请先登录");
        }
        ShopCart shopCart = new ShopCart();
        shopCart.setUserid(user.getUserid());
        shopCart.setGoodsid(goodsid);
        shopCart.setGoodsnum(num);
        shopCartService.updateCartByKey(shopCart);
        return Msg.success("更新购物车成功");
    }

    @RequestMapping("/addCart")
    @ResponseBody
    public Msg addCart(Integer goodsId, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
//        User user = new User();
//        user.setUserid(7);
        if(user == null) {
            return Msg.fail("请先登录");
        }
        //判断是否已经加入购物车
        ShopCart shopCart = shopCartService.selectCartByKey(new ShopCartKey(user.getUserid(), goodsId));
        if (shopCart != null) {
            shopCart.setGoodsnum(shopCart.getGoodsnum()+1);
            shopCartService.updateCartByKey(shopCart);
        }else{
            shopCart = new ShopCart();
            //用户
            shopCart.setUserid(user.getUserid());

            //加入时间
            shopCart.setCatedate(new Date());

            shopCart.setGoodsnum(1);
            shopCart.setGoodsid(goodsId);

            shopCartService.addShopCart(shopCart);
        }
        return Msg.success("加入购物车成功");
    }
}
